package com.ksd.quartz.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class SynchFilesTool {
//    public static void syncMovies(String httpUrl,String localDir) {
//        try {
//            // 获取远程文件列表
//            List<String> remoteFiles = getRemoteFiles(httpUrl);
//            // 获取本地文件列表
//            List<String> localFiles = getLocalFiles(localDir);
//
//            // 找出需要同步的文件
//            for (String remoteFile : remoteFiles) {
//                if (!localFiles.contains(remoteFile)) {
//                    // 下载需要同步的文件
//                    downloadFile(httpUrl  + remoteFile, localDir + remoteFile);
//                    System.out.println("Downloaded: " + remoteFile);
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    private static List<String> getRemoteFiles(String urlStr) throws IOException {
        List<String> files = new ArrayList<>();
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 简单的解析，假设响应中包含文件名
                if (line.contains("<a href")) {
                    int start = line.indexOf(">") + 1;
                    int end = line.lastIndexOf("<");
                    String fileName = line.substring(start, end);
                    if (fileName.endsWith(".mp4") || fileName.endsWith(".avi")) {
                        files.add(fileName);
                    }
                }
            }
        }
        return files;
    }

    private static List<String> getLocalFiles(String localDir) {
        List<String> files = new ArrayList<>();
        Path path = Paths.get(localDir);
        if (Files.exists(path) && Files.isDirectory(path)) {
            try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
                for (Path entry : stream) {
                    if (Files.isRegularFile(entry)) {
                        files.add(entry.getFileName().toString());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return files;
    }

//    private static void downloadFile(String remoteUrl, String localPath) throws IOException {
//        URL url = new URL(remoteUrl);
//        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//        connection.setRequestMethod("GET");
//
//        try (InputStream in = connection.getInputStream();
//             FileOutputStream out = new FileOutputStream(localPath)) {
//            byte[] buffer = new byte[4096];
//            int bytesRead;
//            while ((bytesRead = in.read(buffer)) != -1) {
//                out.write(buffer, 0, bytesRead);
//            }
//        }
//    }
}
